import { _decorator, Component, Node, Vec3 } from 'cc';
const { ccclass, property } = _decorator;


@ccclass('JoyStick')
export class JoyStick extends Component {
    //虚拟摇杆组件
    joyStick: any = null;

    //角色
    @property(Node)
    role: Node = null;
    //速度
    speed: Vec3 = new Vec3(6, 6, 0);

    onLoad() {
        this.joyStick = this.node.getComponent("JoyStickPannl");
    }

    start() {

    }

    update() {
        if (this.joyStick.moving) {
            //根据角度移动
            let x = this.role.getPosition().x + Math.cos(this.joyStick.angle) * this.speed.x;
            let y = this.role.getPosition().y + Math.sin(this.joyStick.angle) * this.speed.y;
            //根据向量移动
            // let x = this.role.getPosition().x+this.joyStick.dir.x*this.speed.x;
            // let y = this.role.getPosition().y+this.joyStick.dir.y*this.speed.y;
            if (x >= 750 || y >= 750 || x <= -750 || y <= -750) {
                return
            }
            this.role.setPosition(new Vec3(x, y, 0));
        }
    }
}